package com.axs.logcollector.axs;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.axs.logcollector.utils.LogCollectorUtils;

public class AxSManagementService{
	private static final Logger m_log = Logger.getLogger(AxSManagementService.class);
	
	private static final String FIND_ALL_CLUSTER_NODE = "findAllClusterNodes";
	public static final String CLUSTER_SERVICE_NAME = "application.cluster:service=ClusterService";
	public static final String AMS_LOG_DIR = "AMS_LOG_DIR";
	private LogCollectionHandler m_logHandler; 
	
	public AxSManagementService() {
		m_logHandler = new LogCollectionHandler();
	}
	
	public List<String> getAllServerIps(String serverIp) {
		String nodeIdResult = m_logHandler.executeCommand(serverIp, findAllClusterNodesCommand());
		List<String> serverIpList = new ArrayList<String>();
		for (String nodeId : LogCollectorUtils.convertToServerIpList(nodeIdResult)) {
			serverIpList.add(LogCollectorUtils.getIpAddress(nodeId));
		}
		m_log.info("AMS server: " + serverIpList);
		return serverIpList;
	}
	
	public Map<String, List<String>> getAllServerIpsAndLogs(String serverIp) {
		List<String> serverIps = getAllServerIps(serverIp);
		Map<String, List<String>> serverIpsAndLogs = new HashMap<String, List<String>>(); 
		for (String server : serverIps) {
			String amsLogDir = getAMSLogDirectory(server);
			List<String> allLogFiles = getAllLogFiles(server, amsLogDir);
			serverIpsAndLogs.put(server, allLogFiles);
		}
		m_log.info("AMS server and conresponding logs: " + serverIpsAndLogs);
		return serverIpsAndLogs;
	}
	
	public String getAMSLogDirectory(String serverIp) {
		String command = ". $HOME/.profile && echo $" + AMS_LOG_DIR;
		String logDir = m_logHandler.executeCommand(serverIp, command);
		return logDir.replaceAll("\n", "");
	}
	
	public List<String> getAllLogFiles(String serverIP, String amsLogDir) {
		String command = ". $HOME/.profile && cd " + amsLogDir + " && ls" ; 
		String logNameResult = m_logHandler.executeCommand(serverIP, command);
		List<String> logNames = Arrays.asList(logNameResult.split("\n"));
		return LogCollectorUtils.buildFullAmsLogDir(amsLogDir, logNames);
	}
	
	public String copyLogsToLocal(String serverIp, List<String> selectedLogs) {
		String amsTmpDir = LogCollectorUtils.getTmpLogFileDir() + File.separator + serverIp;
		File file = new File(amsTmpDir);
		if (!file.exists()) {
			m_log.info("Creating tmp dir " + amsTmpDir + " for server IP " + serverIp);
			file.mkdir();
		}
		for (String logFile : selectedLogs) {  
			String localDestFile = amsTmpDir + File.separator + new File(logFile).getName();
			try {
				m_logHandler.coppyLogsToLocal(serverIp, logFile, localDestFile);
				m_log.info("Coping log file from " + logFile  + " to " + localDestFile);
			} catch (Exception e) {
				m_log.info("Error in coppy file from " + serverIp + " to local " + amsTmpDir + ": " + e);
			}
		}
		return LogCollectorUtils.getTmpLogFileDir();
	}
	
	public void copyLogsToRemote(String remoteIp) {
		
	}
	
	public void closeAllConnections() {
		m_logHandler.closeAllConnections();
	}
	
	private String findAllClusterNodesCommand() {
		String command = " $JBOSS_HOME/bin/twiddle.sh -Djavax.net.ssl.trustStore=$PLATFORM_SCRIPTS_DIR/keystore " +
				"-Djavax.net.ssl.keystore=$PLATFORM_SCRIPTS_DIR/keystore -Djavax.net.ssl.trustStorePassword=changeit " +
				"-usystem -psystem invoke \"" + CLUSTER_SERVICE_NAME + "\" " + FIND_ALL_CLUSTER_NODE;
		return ". $HOME/.profile && " + command;
	}
	
	public static void main(String[] args) {
	}
}
